﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Entity;

namespace BookManageSys.Admin
{
    public partial class BookManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            ProductsBusiness pb = new ProductsBusiness();
            DataTable dt = pb.GetAllData();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ProductsEntity pe = new ProductsEntity();
            pe.Productname = TxtSearch.Text;
            ProductsBusiness pb = new ProductsBusiness();
            DataTable dt = pb.SearchAllData(pe);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");
                if (ChkAll.Checked)
                {
                    chkSelect.Checked = true;
                }
                else
                {
                    chkSelect.Checked = false;
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "删除")
            {
                ProductsEntity pe = new ProductsEntity();
                pe.Productid = Convert.ToInt32(e.CommandArgument);
                ProductsBusiness pb = new ProductsBusiness();
                int res = pb.DeleteProduct(pe);
                if (res > 0)
                {
                    Response.Write("<script>alert('删除成功')</script>");
                    BindGrid();
                }
                else
                {
                    Response.Write("<script>alert('删除失败')</script>");
                }
            }
            if (e.CommandName == "更新")
            {
                Response.Redirect("BookEdit.aspx?productid="+e.CommandArgument);
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            string sql = "";

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");

                if (chkSelect.Checked)
                {
                    // 获取对应行的第2列数据
                    string data = row.Cells[1].Text; // 获取第2列数据
                    sql += " or productid = " + data;
                }
            }

            try
            {
                ProductsBusiness pb = new ProductsBusiness();
                int res = pb.DeleteMoreProducts(sql);
                if (res > 0)
                {
                    Response.Write("<script>alert('删除成功')</script>");
                    BindGrid();
                }
                else
                {
                    Response.Write("<script>alert('删除失败')</script>");
                }
            }
            catch
            {
                Response.Write("<script>alert('不可删除，存在主外键关联！')</script>");
            }
        }

        protected void CheckBox1_CheckedChanged1(object sender, EventArgs e)
        {
            if (ChkPage.Checked)
            {
                PnlPage.Visible = true;
                TxtPageNum.Text = "1";
                int pageNum = Convert.ToInt32(TxtPageNum.Text) - 1;
                SelectPage(pageNum);
            }
            else
            {
                PnlPage.Visible = false;
                BindGrid();
            }
        }
        private bool SelectPage(int pageNum, int pageCount = 10, bool last = false)
        {
            if (last)
            {
                pageNum = pageNum - 1;
            }
            ProductsBusiness pb = new ProductsBusiness();
            DataTable dt = pb.GetPageData(pageNum * pageCount, pageCount);
            if (dt != null && dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
                return true;
            }else
            {
                return false;
            }
        }
        protected void BtnNextPage_Click(object sender, EventArgs e)
        {
            int pageNum = Convert.ToInt32(TxtPageNum.Text);
            int pageCount = Convert.ToInt32(TxtCount.Text);
            bool flag = SelectPage(pageNum, pageCount);
            if (flag)
            {
                TxtPageNum.Text = (Convert.ToInt32(TxtPageNum.Text) + 1).ToString();
            }
        }

        protected void BtnLastPage_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(TxtPageNum.Text) > 1)
            {
                int pageNum = Convert.ToInt32(TxtPageNum.Text) - 1;
                int pageCount = Convert.ToInt32(TxtCount.Text);
                SelectPage(pageNum, pageCount, true);
                TxtPageNum.Text = (Convert.ToInt32(TxtPageNum.Text) - 1).ToString();
            }
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            int pageNum = 0;
            int pageCount = Convert.ToInt32(TxtCount.Text);
            SelectPage(pageNum, pageCount, default);
        }
    }
}